<?php
/**
 * @author Ningjing Chen 2009/10/20
 *
 */
class Elements {
	/**
	 * @return email field
	 */
	public function getEmailTextField($user)
	{
		$element = new Zend_Form_Element_Text('email');
		if (isset($user)) {
			$element->setAttrib('enabled', false);
			$element->setAttrib('readonly', true);
			$element->setValue($user->email);
		}
		$element->setLabel('您的Email地址:*');
		$element->setDescription('激活邮件会被发送到此地址');
		$element->setRequired(true);
						
		$element->addValidator(new Zend_Validate_EmailAddress());
		
		$element->addFilter(new Zend_Filter_HtmlEntities());
		$element->addFilter(new Zend_Filter_StripTags());
		$element->addValidator('NotEmpty');
		return $element;
	}
	
	/**
	 * @return username field
	 */
	public function getUsernameTextField()
	{
		$element = new Zend_Form_Element_Text('username');		
		$element->setLabel('您的真实姓名:*');
		$element->setDescription('2个字以上，让朋友和合作伙伴能更方便地找到您');
		$element->setRequired(true);
		
		$element->addValidator(new Zend_Validate_StringLength(2,20));
	
		$element->addFilter(new Zend_Filter_HtmlEntities());
		$element->addFilter(new Zend_Filter_StripTags());
		
		return $element;
	}
	
	/**
	 * @return password field
	 */
	public function getPasswordTextField()
	{
		$element = new Zend_Form_Element_Password('password');		
		$element->setLabel('想个保险点的密码吧:*');
		$element->setDescription('6位以上，请使用英文字母、符号或数字');
		$element->setRequired(true);
		
		$element->addValidator(new Zend_Validate_StringLength(6,20));
		
		$element->addFilter(new Zend_Filter_HtmlEntities());
		$element->addFilter(new Zend_Filter_StripTags());
		
		return $element;
	}
	
	/**
	 * @return password field
	 */
	public function getConfirmTextField()
	{
		$element = new Zend_Form_Element_Password('confirm');
		$element->setLabel('再输一遍密码:*');
		$element->setRequired(true);
		
		$element->addValidator(new Zend_Validate_StringLength(6,20));
		
		$element->addFilter(new Zend_Filter_HtmlEntities());
		$element->addFilter(new Zend_Filter_StripTags());
		
		return $element;
	}
	
	public function getHiddenField($user) {
		$element = new Zend_Form_Element_Hidden('hasemail');
		$element->setAttrib('id', 'hasemail');
		if ($user) {
			$element->setValue(1);
		} else {
			$element->setValue(0);
		}
		return $element;
	}
	
	/**
	 * @param $invitation invitation
	 * @return invitation title field
	 */
	public function getInvitationTitleTextField($invitation) {
		$element = new Zend_Form_Element_Text('title');		
		$element->setLabel('活动的标题:*');
	//	$element->setDescription('如：红鱼鱼的生日聚会');
		$element->setRequired(true);
		$element->setAttrib('default','如：红鱼鱼的生日聚会');
		if (isset($invitation)) {
			$element->setValue($invitation->title);
			$element->setAttrib('enabled', false);
			$element->setAttrib('readonly', true);
		}
	
		$element->addFilter(new Zend_Filter_HtmlEntities());
		$element->addFilter(new Zend_Filter_StripTags());
		
		return $element;
	}	

	/**
	 * @param $invitation invitation
	 * @return invitation description field
	 */
	public function getInvitationDescriptionTextareaField($invitation)
	{
		$element = new Zend_Form_Element_Textarea('description');		
		$element->setLabel('活动的内容描述:*');
		$element->setAttrib('default','如：红鱼鱼一岁的生日，欢迎帮助过它的朋友们!');
	//	$element->setDescription('如：红鱼鱼一岁的生日，欢迎帮助过它的朋友们！');
		$element->setRequired(true);
		$element->setAttrib('rows',4);
		if (isset($invitation)) {
			$element->setValue($invitation->description);
		}
	
		$element->addFilter(new Zend_Filter_HtmlEntities());
		$element->addFilter(new Zend_Filter_StripTags());
		
		return $element;
	}	
	
	/**
	 * @param $invitation invitation
	 * @return invitation address field
	 */
	public function getInvitationAddressTextareaField($invitation) {
		$element = new Zend_Form_Element_Textarea('address');		
		$element->setLabel('活动的具体地点:*');
		$element->setAttrib('default','如：南京西路39号304室');
	//	$element->setDescription('如：南京西路39号304室');
		$element->setRequired(true);
		$element->setAttrib('rows',3);
		if (isset($invitation)) {
			$element->setValue($invitation->address);
		}
	
		$element->addFilter(new Zend_Filter_HtmlEntities());
		$element->addFilter(new Zend_Filter_StripTags());
		
		return $element;
	}

	/**
	 * @param $invitation invitation
	 * @return invitation date field
	 */
	public function getInvitationDateTextField($invitation)
	{
		$element = new Zend_Form_Element_Text('date');		
		$element->setLabel('活动的时间:* ');
		$element->setAttrib('default','如：2012-12-12 20:00:00 ');
	//	$element->setDescription('如：2010-02-14');
		$element->setRequired(true);
		if (isset($invitation)) {
			$element->setValue($invitation->date);
		}
	
		$element->addFilter(new Zend_Filter_HtmlEntities());
		$element->addFilter(new Zend_Filter_StripTags());
		
		return $element;
	}
	
    /**
     * @param $categories categories avaiable
     * @param $selectedCategory selected category
	 * @return invitation category select
	 */
	public function getInvitationCategorySelect($categories, $selectedCategory)
	{		
		$element = new Zend_Form_Element_Select("category");
		$element->setLabel('活动的类别:*');
		$element->addMultiOptions($this->convertRowSetToArray($categories, 'id', 'name'));
		if (isset($selectedCategory)) {
			$element->setValue($selectedCategory->id);
		}
		return $element;
	}
	

    /**
     * @param $themes themes available
     * @param $selectedTheme selected theme
	 * @return invitation theme select
	 */
	public function getInvitationThemeSelect($themes, $selectedTheme)
	{		
		/* $element = new Zend_Form_Element_Radio("theme");
		$element->addMultiOptions($this->convertRowSetToArray($themes, 'id', 'name'));
		$element->setLabel("邀请函的主题背景:*");
		
		if (isset($selectedTheme)) {
			$element->setValue($selectedTheme->id);
		}*/
		
		$element = new Zend_Form_Element_Text("theme");
		$element->setLabel("邀请函的主题背景:*");
		
	    if (isset($selectedTheme)) {
			$element->setValue($selectedTheme->id);
		}
		return $element;
	}
	
	/**
	 * @return comment text field
	 */
	public function getCommentTextField() {
		$element = new Zend_Form_Element_Text('comment');
		$element->setRequired(true);
		$element->addValidator(new Zend_Validate_StringLength(5,500));
		$element->addFilter(new Zend_Filter_HtmlEntities());
		$element->addFilter(new Zend_Filter_StripTags());
		return $element;
	}
	
	/**
	 * @param $userList user list
	 * @return return user list text area
	 */
	public function getInvitationUsersTextareaField($userList) {
		$element = new Zend_Form_Element_Textarea("users");
		$element->setLabel("填入受邀人的邮件地址 (用','隔开)");
		$element->isRequired(true);
		
		$userString = '';
		if (isset($userList)) {
			$num = count($userList);
			$index=1;
			foreach ($userList as $user) {
				if ($index < $num) {
					$userString.=($user->email.",\n");
				} else {
					$userString.=$user->email;
				}
				$index++;
			}
		}
		$element->setAttrib('rows',5);
		$element->setValue($userString);
//		$element->addValidator(new Zend_Validate_EmailAddress());
		return $element;
	}
	
	/**
	 * Convert data rows set to array, with column1 as key and column2 as value.
	 * 
	 * @param $data data set
	 * @param $column1 column1 from data set, used as key
	 * @param $column2 column2 from data set, used as value
	 */
	private function convertRowSetToArray($data, $column1, $column2) {
		$result = array();
		$result[-1] = '=Select Category=';
		foreach ($data as $entry) {
			$result[$entry->$column1] = $entry->$column2;
		}
		return $result;
	}
	
	/**
	 * Text field for user relation alias.
	 * 
	 * @return Zend_Form_Element_Text
	 */
	public function getContactAliasTextField($userRelation) {
		$element = new Zend_Form_Element_Text('alias');		
		$element->setLabel('呢称');
		$element->setDescription('2个字以上，让朋友和合作伙伴能更方便地找到您');
		$element->setRequired(false);
		if (isset($userRelation)) {
			$element->setValue($userRelation->alias);
		}
		
		$element->addValidator(new Zend_Validate_StringLength(2,20));
	
		$element->addFilter(new Zend_Filter_HtmlEntities());
		$element->addFilter(new Zend_Filter_StripTags());
		
		return $element;
	}
	
}